package com.googlecode.jawb.tests;

import static org.junit.Assert.*;

import java.io.ByteArrayInputStream;
import java.io.IOException;

import org.junit.Test;

import com.googlecode.jawb.BinaryType;
import com.googlecode.jawb.JAWB;
import com.googlecode.jawb.ListenerTarget;
import com.googlecode.jawb.clues.SetArray;
import com.googlecode.jawb.clues.SetItemPostVisitListener;
import com.googlecode.jawb.clues.SetPostVisitListener;
import com.googlecode.jawb.clues.SetPreVisitListener;
import com.googlecode.jawb.clues.SetType;

public class Visitors {
	@SetType(type = BinaryType.UINT_8)
	@SetPreVisitListener()
	@SetPostVisitListener(methodName = "postVisit")
	private short	value;
	
	@SetType(type = BinaryType.UINT_8)
	@SetArray(staticLength = 4)
	@SetItemPostVisitListener(target = ListenerTarget.INPUT)
	private byte[]	i1;
	
	private boolean	preVisitValue	= false;
	private boolean	postVisitValue	= false;
	
	@SuppressWarnings("unused")
	private void onPreVisitValue() {
		preVisitValue = true;
	}
	
	@SuppressWarnings("unused")
	private void postVisit() {
		postVisitValue = true;
	}
	
	@SuppressWarnings("unused")
	private int onItemPostVisitI1(int index, ListenerTarget currentOperation) {
		if (currentOperation == ListenerTarget.INPUT) {
			return index + 1;
		}
		
		return index;
	}
	
	@Test()
	public void input() throws IOException {
		Visitors result = (Visitors) new JAWB().loadFrom(
			new ByteArrayInputStream(
				new byte[]{
					(byte) 0xAA,
					(byte) 0xAA, (byte) 0xBB
				}
			),
			Visitors.class
		);
		
		assertEquals(result.preVisitValue, true);
		assertEquals(result.postVisitValue, true);
		assertArrayEquals(new byte[]{(byte) 0xAA, 0x00, (byte) 0xBB, 0x00}, result.i1);
	}
}
